package com.lfg.other;

import com.alibaba.fastjson.JSON;

import java.util.HashSet;
import java.util.Set;

public class FindSwapValues {


    public static void main(String[] args) {
        int[] ret = findSwapValues(new int[]{4, 1, 2, 1, 1, 2}, new int[]{3, 6, 3, 3});
        System.out.println("[" + JSON.toJSONString(ret) + "]");


        int[] ret2 = findSwapValues(new int[]{1,2,3}, new int[]{4,5,6});
        System.out.println("[" + JSON.toJSONString(ret2) + "]");
        // 6 15  9
    }

    public static int[] findSwapValues(int[] array1, int[] array2) {

        int total1 = 0;
        Set<Integer> data = new HashSet<>();
        for (int i = 0; i < array1.length; i++) {
            total1 += array1[i];
            data.add(array1[i]);
        }
        int total2 = 0;
        for (int i = 0; i < array2.length; i++) {
            total2 += array2[i];
        }
        int diff = (total1 - total2);
        if(diff%2 != 0){
            return new int[]{};
        }
        diff = diff/2;

        for (int i = 0; i < array2.length; i++) {
            int value = array2[i] + diff;
            if (data.contains(value)) {
                return new int[]{value, array2[i]};
            }
        }

        return new int[]{};
    }
}
//
// 11 16  = 4